package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.Notice;
import cn.com.dcsgo.dto.NoticeAddDTO;
import cn.com.dcsgo.dto.NoticeQueryDTO;
import cn.com.dcsgo.dto.NoticeUpdateDTO;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.model.Result;
import cn.com.dcsgo.service.NoticeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告相关功能接口
 *
 * @author Dcsgo
 * @since 2025/10/26 13:31
 */
@Tag(name = "公告相关功能接口")
@RestController
@RequestMapping("shop/notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    @Operation(summary = "分页获取公告", description = "公告数据按创建时间逆序排序")
    public Result<Page<Notice>> getPage(PageDTO pageDTO, NoticeQueryDTO noticeQueryDTO) {
        return Result.success(noticeService.getPage(pageDTO, noticeQueryDTO));
    }

    @GetMapping("info/{noticeId}")
    @Operation(summary = "获取公告", description = "根据公告ID获取，小程序也用该接口不做权限限制")
    public Result<Notice> getNotice(@PathVariable Long noticeId) {
        return Result.success(noticeService.getById(noticeId));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('shop:notice:save')")
    @Operation(summary = "添加公告", description = "默认为公布，不置顶")
    public Result<Void> addNotice(@Validated @RequestBody NoticeAddDTO noticeAddDTO) {
        return Result.handle(noticeService.save(noticeAddDTO));
    }

    @PutMapping
    @PreAuthorize("hasAuthority('shop:notice:update')")
    @Operation(summary = "修改公告")
    public Result<Void> updateNotice(@Validated @RequestBody NoticeUpdateDTO noticeUpdateDTO) {
        return Result.handle(noticeService.update(noticeUpdateDTO));
    }

    @DeleteMapping("{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:delete')")
    @Operation(summary = "删除公告", description = "根据公告ID")
    public Result<Void> deleteNotice(@PathVariable Long noticeId) {
        return Result.handle(noticeService.removeById(noticeId));
    }

    @GetMapping({"noticeList","topNoticeList"})
    @Operation(summary = "获取超级店铺公告列表",description = "状态为正常，按是否置顶及更新时间逆序排序")
    public Result<List<Notice>> getSupperNoticeList(){
        return Result.success(noticeService.getSupperNoticeList());
    }

}
